package com.falcon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.falcon.core.utils.BeanUtils;
import com.falcon.domain.dos.RoleDO;
import com.falcon.domain.po.RolePO;
import com.falcon.domain.po.UserRolePO;
import com.falcon.domain.req.RoleQuery;
import com.falcon.domain.req.RoleRequest;
import com.falcon.mapper.IRoleMapper;
import com.falcon.mapper.IUserRoleMapper;
import com.falcon.mvc.domain.BasePager;
import com.falcon.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swan.mybatis.condition.Condition;
import com.swan.mybatis.condition.OpType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class RoleService implements IRoleService {

    @Autowired
    private IRoleMapper roleMapper;

    @Autowired
    private IUserRoleMapper userRoleMapper;

    @Override
    public Long save(RoleRequest request){
        RolePO rolePO = BeanUtil.copyProperties(request, RolePO.class);

        this.roleMapper.insertNotNull(rolePO);

        return rolePO.getId();
    }

    @Override
    public Boolean update(Long id, RoleRequest request){

        RolePO rolePO = BeanUtil.copyProperties(request, RolePO.class);
        rolePO.setId(id);

        return this.roleMapper.updateNotNull(rolePO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.roleMapper.deleteById(id);
    }

    @Override
    public RoleDO queryById(Long id){

        RolePO rolePO = this.roleMapper.selectById(id);

        RoleDO roleDO = BeanUtil.copyProperties(rolePO, RoleDO.class);

        return roleDO;
    }


    @Override
    public List<RoleDO> queryList(RoleQuery request){

        Condition condition = request.toCondition();

        List<RolePO> poList = this.roleMapper.selectList(condition);

        List<RoleDO> doList = BeanUtil.copyToList(poList, RoleDO.class);

        return doList;
    }

    @Override
    public BasePager<RoleDO> queryPager(RoleQuery request){


        PageInfo<RolePO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.roleMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, RoleDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.roleMapper.deleteByIds(ids);
    }

    @Override
    public List<RolePO> queryRoleListByUserId(Long userId) {

        List<UserRolePO> userRoleList = this.userRoleMapper.selectList(Condition.newInstance().and(UserRolePO.Fields.userId, OpType.equals, userId));
        if (userRoleList.isEmpty()) {
            return Collections.emptyList();
        }

        List<Long> roleIds = userRoleList.stream().map(UserRolePO::getRoleId).collect(Collectors.toList());

        List<RolePO> rolePOS = this.roleMapper.selectList(Condition.newInstance().and(RolePO.Fields.id, OpType.in, roleIds));

        return rolePOS;
    }


}

